<?php

namespace api\energy\common;

use api\energy\Config;
use api\energy\Sign;

class Http
{

    public static function writeLog($str)
    {
        Log::writeLog(date('Y-m-d'), $str);
    }

    /**
     * 常规请求
     *
     * @param $method
     * @param $url
     * @param $param
     * @param $headers
     * @return mixed
     */
    public static function doCommHttp($method, $url, $param = [], $headers = [])
    {
        self::writeLog('请求地址: [' . $method . ']' . Config::getHost() . $url);
        $headers = self::buildHeader($headers);
        $requestData = self::buildRequestData($param);
        self::writeLog('请求标头: ' . json_encode($headers, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES));
        self::writeLog('请求参数: ' . json_encode($requestData, JSON_UNESCAPED_UNICODE));

        $requestStartTime = microtime(true);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($method == 'POST' || $method == 'PUT') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_URL, Config::getHost() . $url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
        } else {
            curl_setopt($ch, CURLOPT_URL, Config::getHost() . $url . '?' . http_build_query($requestData));
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);
        $httpData = curl_getinfo($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $return = json_decode($result, true);
        self::writeLog('响应结果' . '(' . round(microtime(true) - $requestStartTime, 3) . '秒' . ')' . ': [' . $httpCode . ']' . ($return ? json_encode($return, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES) : $result) . PHP_EOL);
        return $return;
    }

    /**
     * 构造请求数据
     *
     * @param array $param 请求数据
     * @return array
     */
    public static function buildRequestData($param)
    {
        ksort($param);
        $requestData = [
            'appid' => Config::get('appid'),
            'timestamp' => time(),
            'data' => $param
        ];
        self::writeLog('签名数据: ' . json_encode($requestData, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        $requestData['sign'] = Sign::generateSign($requestData);
        self::writeLog('得到签名: ' . $requestData['sign']);

        return $requestData;
    }

    /**
     * 构造请求头信息
     *
     * @param array $otherHeaders 自定义请求头参数
     * @return array
     */
    public static function buildHeader($otherHeaders = [])
    {
        $defaultHeaders = [
            'Content-Type' => 'application/json',
            'charset' => 'UTF-8'
        ];
        $headers = array_merge($defaultHeaders, $otherHeaders);

        $return = [];
        foreach ($headers as $k => $v) {
            $return[] = $k . ':' . $v;
        }
        return $return;
    }

}